// TESTPARSER CLASS USED TO TEST ONLY PART 4 OF CPS710 ASSIGNMENT 2
//
class TestParser {

    private static SimpleCharStream instream;
    private static ParserTokenManager scanner;
    private static Parser parser;

    public static void main(String args[]) {

		// Figure out input stream from args or stdin
		java.io.InputStream infile;
		if (args.length < 1) {
	  	infile = System.in;
		} 
		else try {  
	   	infile = new java.io.FileInputStream(args[0]);
		} catch (java.io.FileNotFoundException e) {
	    	System.out.println("File " + args[0] + " not found."); 
	    	return;
		} 

		// Create scanner and parser
		instream = new SimpleCharStream(infile);
		scanner = new ParserTokenManager(instream);
		parser = new Parser(scanner);

		// Call parser until EOF reached
		while (nextParseSkipErrors());
    	}


    private static boolean nextParseSkipErrors() {
		SimpleNode tree;
		try {
			tree = parser.start();
			tree.dump("");
			System.out.println();

		// Handle Lexical Errors
		} catch (TokenMgrError e) {
			System.out.println(e.toString());
			return false;
				
		// Handle Syntax Errors
		} catch (ParseException e) {
			if (e.currentToken.next.kind != ParserConstants.EOF)
				System.out.println(e.toString());
			return false;
		}
		return true;
	}
}
